package com.zx.idc.backend.gui.base.oper;


import com.baomidou.mybatisplus.entity.Columns;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zx.idc.backend.gui.base.oper.service.BaseOperCompanyReportService;
import com.zx.idc.backend.gui.common.BaseApi;
import com.zx.idc.backend.gui.common.Result;
import com.zx.idc.backend.gui.common.base.BaseExcelApi;
import com.zx.idc.backend.gui.common.base.BaseUpdateApi;
import com.zx.idc.ds.base.oper.entity.BaseOperCompany;
import com.zx.idc.ds.base.oper.service.IBaseOperCompanyService;
import com.zx.idc.ds.base.oper.vo.BaseOperCompanyVo;
import com.zx.idc.ds.common.constant.DataFeatureConstant;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 经营单位信息管理 前端控制器
 * </p>
 *
 * @author rxliuli
 * @since 2019-01-31
 */
@RestController
@RequestMapping("/api/baseOperCompany")
public class BaseOperCompanyController extends BaseApi implements BaseUpdateApi<IBaseOperCompanyService, BaseOperCompany>, BaseExcelApi<IBaseOperCompanyService, BaseOperCompanyVo> {
    @Autowired
    private IBaseOperCompanyService service;
    @Autowired
    private BaseOperCompanyReportService baseOperCompanyReportService;

    @Override
    @RequiresPermissions("basic-data:base-oper-company:edit")
    public Result<Boolean> update(@RequestBody BaseOperCompany entity) {
        String msg;
        try {
            return success(service.updateBaseOperCompany(entity));
        } catch (Exception e) {
            msg = e.getMessage();
            log.error("修改经营单位异常", e);
        }
        return error(msg);
    }

    /**
     * 获取一个 Service，由子类实现
     * 实现这个方法可以更快的获取到 Service 以提高响应速度
     *
     * @return 服务类实现对象
     */
    @Override
    public Optional<IBaseOperCompanyService> getService() {
        return Optional.of(service);
    }

    /**
     * 查询一页的数据
     *
     * @param page   分页信息
     * @param entity 实体参数
     * @return 查询到的分页数据
     */
    @RequiresPermissions("basic-data:base-oper-company")
    @GetMapping("/query-page")
    public Result<Page<BaseOperCompanyVo>> queryPage(Page<BaseOperCompanyVo> page, BaseOperCompanyVo entity) {
        return success(service.queryPage(page, entity));
    }

    @GetMapping("/get/{id}")
    @RequiresPermissions("basic-data:base-oper-company")
    public Result<BaseOperCompanyVo> get(@PathVariable("id") Long id) {
        return success(service.getById(id));
    }


    @GetMapping("/select-list")
    @RequiresPermissions("basic-data:base-oper-company")
    public Result<List<BaseOperCompany>> listByIdAndName() {
        return success(service.selectList(new EntityWrapper<BaseOperCompany>().setSqlSelect(Columns.create().column("idcId").column("operName"))));
    }

    @PostMapping("/insert-report/{idcId}")
    @RequiresPermissions("basic-data:base-oper-company:edit")
    public Result<Boolean> insertReport(@PathVariable String idcId) {
        return success(baseOperCompanyReportService.insertReport(idcId));
    }

    @PostMapping("/update-report/{idcId}")
    @RequiresPermissions("basic-data:base-oper-company:edit")
    public Result<Boolean> updateReport(@PathVariable String idcId) {
        return success(baseOperCompanyReportService.updateReport(idcId));
    }

    @PostMapping("/delete/batch")
    @RequiresPermissions("basic-data:base-oper-company:edit")
    public Result<Boolean> delete(@RequestBody List<String> idList) {
        String msg;
        try {
            final boolean[] result = {true};
            result[0] = service.deleteBatchByIds(idList);
            idList.forEach(idcId -> {
                if (service.selectOneIgnoreDelFlagByIdcId(idcId).getDataFeature() == DataFeatureConstant.DELETE
                    && !baseOperCompanyReportService.deleteReport(idcId)) {
                    result[0] = false;
                }
            });
            return success(result[0]);
        } catch (Exception e) {
            msg = e.getMessage();
            log.error("删除经营单位异常", e);
        }
        return error(msg);
    }

    @PostMapping("/add")
    @RequiresPermissions("basic-data:base-oper-company:edit")
    public Result<Boolean> add(@RequestBody BaseOperCompany entity) {
        String msg;
        try {
            return success(service.add(entity));
        } catch (Exception e) {
            msg = e.getMessage();
            log.error("新增经营单位异常", e);
        }
        return error(msg);
    }

    @GetMapping("/exists-by-id/{idcId}")
    @RequiresPermissions("basic-data:base-oper-company")
    public Result<Boolean> existsById(@PathVariable String idcId) {
        return success(service.existsById(idcId));
    }
}
